package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.CooperateDao;
import com.baor.domain.CooperateVo;
import com.baor.domain.ParkingVo;
import com.baor.service.CooperateService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.baor.utils.DateUtil.now;

@Service
public class CooperateServiceImpl implements CooperateService {

    @Autowired
    private CooperateDao cooperateDao;

    @Override
    public AjaxResult<?> cooperateList(CooperateVo vo) {
        Page<CooperateVo> page = null;
        Map<String,Object> map = new HashMap<>();
        if(vo.getPageIndex() != null){
            page = new Page<>(vo.getPageIndex(),vo.getPageSize());
            map.put("list", cooperateDao.selectPage(page,null).getRecords());
            map.put("pageCount", cooperateDao.selectPage(page, null).getPages());
            return AjaxResult.success(map);
        }
        page = new Page<>(1,10);
        map.put("list", cooperateDao.selectPage(page,null).getRecords());
        map.put("pageCount", cooperateDao.selectPage(page, null).getPages());
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult<?> search(CooperateVo vo) {
        LambdaQueryWrapper<CooperateVo> lqw = new LambdaQueryWrapper<>();
        Page<CooperateVo> page = null;
        Map<String,Object> map = new HashMap<>();
        if(vo.getPageIndex() != null && vo.getCooperateName() != null){
            lqw.like(CooperateVo::getCooperateName, vo.getCooperateName());
            page = new Page<>(vo.getPageIndex(),vo.getPageSize());
            map.put("list", cooperateDao.selectPage(page,lqw).getRecords());
            map.put("pageCount", cooperateDao.selectPage(page, lqw).getPages());
        } else if (vo.getCooperateName() != null) {
            lqw.like(CooperateVo::getCooperateName, vo.getCooperateName());
            page = new Page<>(1,10);
            map.put("list", cooperateDao.selectPage(page,lqw).getRecords());
            map.put("pageCount", cooperateDao.selectPage(page, lqw).getPages());
        }else if(vo.getPageIndex() != null){
            page = new Page<>(vo.getPageIndex(),vo.getPageSize());
            map.put("list", cooperateDao.selectPage(page,null).getRecords());
            map.put("pageCount", cooperateDao.selectPage(page, null).getPages());
        }else{
            page = new Page<>(1,10);
            map.put("list", cooperateDao.selectPage(page,null).getRecords());
            map.put("pageCount", cooperateDao.selectPage(page, null).getPages());
        }
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult<?> insert(CooperateVo cooperateVo) {
        cooperateVo.setCooperateCreatetime(now());
        return cooperateDao.insert(cooperateVo) > 0 ? AjaxResult.success(cooperateVo.getCooperateId()) : AjaxResult.error("插入失败") ;
    }

    @Override
    public AjaxResult<?> update(CooperateVo cooperateVo) {
        return cooperateDao.updateById(cooperateVo) > 0 ? AjaxResult.success() : AjaxResult.error("更新失败") ;
    }

    @Override
    public AjaxResult<?> delete(Integer cooperateId) {
        return cooperateDao.deleteById(cooperateId) > 0 ? AjaxResult.success() : AjaxResult.error("删除失败") ;
    }

    @Override
    public AjaxResult<?> info(Integer cooperateId) {
        return AjaxResult.success(cooperateDao.selectById(cooperateId));
    }


}
